
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import math as math


fname = 'kinmars-cole-3e8-reproduce.out'

def filereader(fname):
    infile = open(fname,'r')
    t,hesc,conc,ttemp,r1,r2,flux,co2,h2,o2,oh = [0],[],[],0,[],[],[],[],[],[],[]
    print(t,len(t))
    while ( (len(t) < 2) | (len(t)<79)):
        temp = infile.readline()
        while 'TSTEP' not in temp:
            temp = infile.readline()

        temp = temp.split()
        ttemp = float(temp[3]) # dt 
        t = t + [max(t)+ttemp] # total run time

        while 'CONDITION' not in temp:
            temp = infile.readline()
        while 'MIXING' not in temp:
            temp = infile.readline()
        while 'H2 ' not in temp:
            temp = infile.readline()
        temp = infile.readline()
        h2 += [float(temp.split()[7])]
        o2 += [float(temp.split()[4])]
        oh += [float(temp.split()[8])]

        while 'CO ' not in temp:
            temp = infile.readline()
        temp = infile.readline()
        conc += [float(temp.split()[2])]
        co2 += [float(temp.split()[3])]

    
    dat= {'esc':hesc,'t':t,'conc':conc,'r1':r1,'r2':r2,'flux':flux,'co2':co2,'h2':h2,'o2':o2,'oh':oh}
    return dat

dat = filereader(fname)

matplotlib.rc('font', family='serif')
matplotlib.rc('xtick', labelsize = 18)
matplotlib.rc('ytick', labelsize = 18)
matplotlib.rc('axes', linewidth=2)
fontname = 'serif'
fontsize=30
lw = 3.3

fig,ax=plt.subplots(figsize=(10,4))
ax.loglog(dat['t'][1:],dat['co2'],linewidth=2,color='green')
ax.loglog(dat['t'][1:],dat['conc'],linewidth=2,color='orange')
ax1 = ax.twinx()
ax1.loglog(dat['t'][1:],dat['h2'],linewidth=2,color='blue')
ax1.loglog(dat['t'][1:],dat['o2'],linewidth=2,color='magenta')


ax.set_xlim([1e2,1e19]) 
ax.set_ylim([1e-3,3e1])
ax1.set_ylim([7e-7,1e-4])
ax.set_xlabel('time in seconds')
ax.set_ylabel('rate')
plt.savefig('cotime.png',dpi=300)

